package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.PageResult;
import com.crm.domain.dto.TransferDto;
import com.crm.domain.entity.Transfer;
import com.crm.domain.vo.BusinessVo;
import com.crm.domain.vo.TransferVo;
import com.crm.mapper.TransferMapper;
import com.crm.service.TransferService;
import com.crm.utils.HttpStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer> implements TransferService {
    private final TransferMapper transferMapper;

    /**
     * 查询转派管理列表
     * @param transferDto
     * @return
     */
    @Override
    public PageResult<TransferVo> queryAll(TransferDto transferDto) {
        Page<Transfer> page = Page.of(transferDto.getPageNum(), transferDto.getPageSize());
        LambdaQueryWrapper<Transfer> wrapper = Wrappers.<Transfer>lambdaQuery()
                .like(ObjectUtils.isNotEmpty(transferDto.getUserName()), Transfer::getUserName, transferDto.getUserName())
                .like(ObjectUtils.isNotEmpty(transferDto.getPhonenumber()), Transfer::getPhonenumber, transferDto.getPhonenumber())
                .ge(ObjectUtils.isNotEmpty(transferDto.getBeginTime()),Transfer::getBeginTime,transferDto.getBeginTime())
                .le(ObjectUtils.isNotEmpty(transferDto.getEndTime()),Transfer::getEndTime,transferDto.getEndTime());

        Page<Transfer> transferPage = transferMapper.selectPage(page, wrapper);

        List<TransferVo> transferVoList = transferPage.getRecords().stream().map(transfer -> {
            TransferVo transferVo = new TransferVo();
            BeanUtil.copyProperties(transfer, transferVo);
            return transferVo;
        }).collect(Collectors.toList());

        PageResult<TransferVo> pageResult = new PageResult<>();
        pageResult.setTotal(transferVoList.size());
        pageResult.setRows(transferVoList);
        pageResult.setCode(HttpStatus.SUCCESS);
        pageResult.setMsg("查询成功");
        return pageResult;
    }
}
